home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / update~4.z / update~4 / lib_stdio__fopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  2.0 KB  |  85 lines

  1. /*            _ f o p e n m o d e
  2.  *
  3.  * This function scans the mode with which a channel should be
  4.  * opened. It then opens the channel using that mode and returns
  5.  * the channel number to the caller. On error, the value -1 will
  6.  * be returned.
  7.  *
  8.  * If the function succeeds, the flags argument will be set to the
  9.  * mode with which the channel was opened. If the fd argument is
  10.  * -1, the channel will be allocated, otherwise the specified channel
  11.  * will be used.
  12.  *
  13.  * Patchlevel 1.1
  14.  *
  15.  * Edit History:
  16.  * 05-Sep-1989    Change SETCLEANUP to SETIOFLUSH.
  17.  */
  18.  
  19.  
  20. #include <fcntl.h>
  21. #include "stdiolib.h"
  22.  
  23. /*LINTLIBRARY*/
  24.  
  25. extern int errno;            /* error code */
  26.  
  27. #define CREATMODE 0666            /* mode to creat file */
  28.  
  29. int _fopen(name, mode, fd, flags)
  30.  
  31. CONST char *name;                /* name of file */
  32. CONST char *mode;                /* mode to open */
  33. int        fd;                    /* allocated channel */
  34. short      *flags;                /* stdio flags */
  35.  
  36. {
  37.   int rw;                /* basic mode */
  38.   int update;                /* read and write required */
  39. #ifndef __STDC__ /* use protos (from std.h) instead */
  40.   int close();                /* close a file */
  41.   int creat();                /* create a file */
  42.   int open();                /* open a file */
  43.   long lseek();                /* seek to position */
  44. #endif
  45.  
  46.   rw     = *mode++;
  47.   update = *mode == '+';
  48.  
  49.   switch (rw) {
  50.  
  51.   case 'w':
  52.     *flags = update ? _IORW : _IOWRITE;
  53.     if (fd == -1) {
  54.       fd = creat(name, CREATMODE);
  55.       if (update && fd != -1 && (fd = close(fd)) != -1)
  56.         fd = open(name, O_RDWR);
  57.     }
  58.     break;
  59.  
  60.   case 'r':
  61.     *flags = update ? _IORW : _IOREAD;
  62.     if (fd == -1)
  63.       fd = open(name, update ? O_RDWR : O_RDONLY);
  64.     break;
  65.  
  66.   case 'a':
  67.     *flags = update ? _IORW : _IOWRITE;
  68.     if (fd == -1) {
  69.       fd = open(name, update ? O_RDWR : O_WRONLY);
  70.       if (fd == -1 && errno == ENOENT) {
  71.         fd = creat(name, CREATMODE);
  72.     if (update && fd != -1 && (fd = close(fd)) != -1)
  73.       fd = open(name, O_RDWR);
  74.       }
  75.     }
  76.     if (fd != -1 && lseek(fd, 0L, SEEK_END) != -1)
  77.       break;
  78.  
  79.   default:
  80.     fd = -1;
  81.   }
  82.  
  83.   return fd;
  84. }
  85.